'use strict';
import request from 'superagent';

export const ApiTable = {
   getTotalDisclosureCount: 'intellectualProperty/getTotalDisclosureCount',
   getDepartmentRanking: 'intellectualProperty/getDepartmentRanking',
   getDetailedStats: 'intellectualProperty/getDetailedStats',
   getAcceptedCasesRanking: 'intellectualProperty/getAcceptedCasesRanking',
   getTypeDistribution: 'intellectualProperty/getTypeDistribution',
   getStats: 'intellectualProperty/getStats',
   getProductRelatedIPStats: 'intellectualProperty/getProductRelatedIPStats',
   
   // 专利管理相关API
   getPatentList: 'patentManagement/list',
   editPatent: 'patentManagement/edit/:id',
   deletePatent: 'patentManagement/delete/:id',
   importPatents: 'patentManagement/import',
   changePatentee: 'patentManagement/changePatentee/:id',
   exportPatents: 'patentManagement/export',
   getPatentTemplate: 'patentManagement/template',
   
   // 软著管理相关API
   getSoftwareCopyrightList: 'softwareCopyright/list',
   createSoftwareCopyright: 'softwareCopyright',
   editSoftwareCopyright: 'softwareCopyright/:id',
   deleteSoftwareCopyright: 'softwareCopyright/:id',
   importSoftwareCopyright: 'softwareCopyright/import',
   exportSoftwareCopyright: 'softwareCopyright/export',
   getSoftwareCopyrightTemplate: 'softwareCopyright/template',

   // 商标管理相关API
   getTrademarkList: 'brandManagement/list',
   getTrademarkDetail: 'brandManagement/:id',
   addTrademark: 'brandManagement',
   updateTrademark: 'brandManagement/:id',
   deleteTrademark: 'brandManagement/:id',
   batchDeleteTrademark: 'brandManagement/batch-delete',
   exportTrademark: 'brandManagement/export',
   importTrademark: 'brandManagement/import',
};

export const RouteTable = {
   fileUpload: '/_upload/new',
   cleanUpUploadTrash: '/_upload/cleanup',
};

const resultHandler = (resolve, reject) => (err, res) => {
   if (err) {
      if (err.status == 401) {
         // 退出到登录页
         const user = JSON.parse(sessionStorage.getItem('user'));
         sessionStorage.clear();
         window.document.location.replace('/login');
         reject('unauth');
      } else {
         reject({
            status: err.status || 0,
            body: err.response ? err.response.body : err.message
         });
      }
      reject({
         status: err.status || 0,
         body: err.response ? err.response.body : err.message
      });
   } else {
      resolve(res.body);
   }
};

export const buildRoute = (url) => {
   const user = JSON.parse(sessionStorage.getItem('user'));
   if (user == null) {
      return url;
   }
   let connector = url.indexOf('?') === -1 ? '?' : '&';
   return `${url}${connector}token=${user.token}`;
};
export class RouteRequest {
   static get = (url, query) =>
      new Promise((resolve, reject) => {
         request.get(buildRoute(url)).query(query).end(resultHandler(resolve, reject));
      });

   static post = (url, data, query) =>
      new Promise((resolve, reject) => {
         request.post(buildRoute(url)).query(query).send(data).end(resultHandler(resolve, reject));
      });

   static delete = (url, data, query) =>
      new Promise((resolve, reject) => {
         request.delete(buildRoute(url)).query(query).send(data).end(resultHandler(resolve, reject));
      });

   static mockPost = (url, data, query, header) => new Promise((resolve, reject) => {
      request.post(url).set(header).query(query).send(data)
         .end(resultHandler(resolve, reject));
   });
}

export class Request {
   static get = (url, query) => new Promise((resolve, reject) => {
      request.get(buildUrl(url)).set(setHeader()).query(query).use(noCache)
         .end(resultHandler(resolve, reject));
   });

   static post = (url, data, query) => new Promise((resolve, reject) => {
      request.post(buildUrl(url)).set(setHeader()).query(query).use(noCache)
         .send(data)
         .end(resultHandler(resolve, reject));
   });

   static put = (url, data, query) => new Promise((resolve, reject) => {
      request.put(buildUrl(url)).set(setHeader()).query(query).use(noCache)
         .send(data)
         .end(resultHandler(resolve, reject));
   });

   static delete = (url) => new Promise((resolve, reject) => {
      request.delete(buildUrl(url)).set(setHeader()).use(noCache).end(resultHandler(resolve, reject));
   });

   static mockGet = (url, query, header) => new Promise((resolve, reject) => {
      request.get(url).set(header).query(query).end(resultHandler(resolve, reject));
   });

   static mockPost = (url, data, query, header) => new Promise((resolve, reject) => {
      request.post(url).set(header).query(query).send(data)
         .end(resultHandler(resolve, reject));
   });
}

